﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using JUtil;
using System.Reflection;

public partial class GridView_PureGridView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    #region grv event handlers
    
    protected void grv_Init(object sender, EventArgs e)
    {
        GridView aGRV = (GridView)sender;
        
        aGRV.AllowPaging=true ;
        aGRV.PageSize=5;
        aGRV.AutoGenerateColumns = false;
        aGRV.DataKeyNames=new string[] {"CustomerID"};

        // validate requiredment before action
        aGRV.RowUpdating += new GridViewUpdateEventHandler(aGRV_RowUpdating);
        aGRV.RowDeleting += new GridViewDeleteEventHandler(aGRV_RowDeleting);

        // handle aODS.UpdateMethod and aODS.DeleteMethod Exception
        aGRV.RowUpdated += new GridViewUpdatedEventHandler(aGRV_RowUpdated);
        aGRV.RowDeleted += new GridViewDeletedEventHandler(aGRV_RowDeleted);
    }

    /// <summary>
    /// 用於在刪除資料前判斷輸入資料是否有效 (若無效，e.Cancel = true)
    /// </summary>
    private void aGRV_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
    }

    /// <summary>
    /// 用於在刪除資料後判斷刪除是否成功
    /// </summary>
    private void aGRV_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        if (e.Exception != null)
        {
            Log.ReportError(e.Exception.InnerException);
            e.ExceptionHandled = true;
        }
        else
        {
            (sender as GridView).DataBind();
        }
    }

    /// <summary>
    /// 用於在異動資料前判斷輸入資料是否有效(若無效，e.Cancel = true)
    /// </summary>
    private void aGRV_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string CustomerID = e.NewValues["CustomerID"] as string;
        string CompanyName = e.NewValues["CompanyName"] as string;

        if (string.IsNullOrEmpty(CompanyName))
        {
            Log.ReportError("CompanyName can not be empty!", "grv_RowUpdating");
            e.Cancel = true;
        }
    }

    /// <summary>
    /// 用於在異動資料前判斷輸入資料是否有效
    /// </summary>
    private void aGRV_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        if (e.Exception != null)
        {
            Log.ReportError(e.Exception.InnerException);
            e.KeepInEditMode = true;
            e.ExceptionHandled = true;
        }
        else
        {
            (sender as GridView).DataBind();
        }
    }

    #endregion
    #region ods event handlers
    
    protected void ods_Init(object sender, EventArgs e)
    {
        ObjectDataSource aODS = (ObjectDataSource)sender;
        
        aODS.DataObjectTypeName="TheDataObject";
        
        aODS.TypeName="TheDataObjectOperator";
        
        aODS.SelectMethod="Select";
        aODS.InsertMethod="Insert";
        aODS.UpdateMethod="Update";
        aODS.DeleteMethod="Delete";
        aODS.SelectCountMethod="SelectCount";
        
        // 設定分頁參數
        aODS.EnablePaging = true;
        aODS.SelectParameters.Add("startRowIndex", TypeCode.Int32, "0");
        aODS.SelectParameters.Add("maximumRows", TypeCode.Int32, "0");

        aODS.OldValuesParameterFormatString = "original_{0}";

        // handle aODS.SelectMethod and aODS.SelectCountMethod Exception
        aODS.Selected += new ObjectDataSourceStatusEventHandler(aODS_Selected);
    }

    private void aODS_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    {
        if (e.Exception != null)
        {
            Log.ReportError(e.Exception.InnerException);
            e.ExceptionHandled = true;
        }
    }

    #endregion

} // end of GridView_PureGridView
